﻿using Kingdee.BOS.JSON;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle
{
    public static class CustCommon
    {
        /// <summary>
        /// 构造WebApi单据查询参数
        /// </summary>
        /// <param name="formId">单据标识</param>
        /// <param name="fieldKeys">查询的字段名</param>
        /// <param name="filterString">查询过滤条件</param>
        /// <returns></returns>
        public static string QueryParameter(string formId, string fieldKeys, string filterString)
        {
            var param = new
            {
                FormId = formId,
                FieldKeys = fieldKeys,
                FilterString = filterString
            };
            return JsonConvert.SerializeObject(param);
        }
        public static Dictionary<string, object> JSONToDictionary(this JSONObject json)
        {
            Dictionary<string, object> d = new Dictionary<string, object>(json, StringComparer.CurrentCultureIgnoreCase);
            return d;
        }
        public static object RKGet(this Dictionary<string, object> dic, string propName)
        {
            object result = null;
            dic.TryGetValue(propName, out result);
            return result;
        }
        /// <summary>
        /// 自定义函数，存在就覆盖，不存在就增加
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void RKAdd(this Dictionary<string, object> dic, string key, object value)
        {
            if (dic.ContainsKey(key))
            {
                dic[key] = value;
            }
            else
            {
                dic.Add(key, value);
            }
        }
    }
}
